﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;

namespace MyWebPagesStarterKit.Rewriter
{
    /// <summary>
    /// SectionHeaderUtils overrides all meta tags in header of a webpage.
    /// </summary>
    public static class SectionHeaderUtils
    {
        /// <summary>
        /// Generates meta data
        /// </summary>
        /// <param name="page">The current page</param>
        /// <param name="title">Header title</param>
        /// <param name="desciption">Page description</param>
        /// <param name="keywords">Page keywords</param>
        public static void GenerateMeta(Page page, string title, string desciption, string keywords)
        {
            page.Title = title;

            HtmlMeta _description = null;
            HtmlMeta _keywords = null;

            ControlCollection collection = page.Header.Controls;
            foreach (Control control in collection)
            {
                if (control.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlMeta")
                {
                    HtmlMeta hmc = (HtmlMeta)control;

                    switch (hmc.Name)
                    {
                        case "description":
                            _description = hmc;
                            _description.Attributes.Add("name", "description");
                            _description.Attributes.Add("content", desciption);
                            break;
                        case "keywords":
                            _keywords = hmc;
                            _keywords.Attributes.Add("name", "keywords");
                            _keywords.Attributes.Add("content", keywords);
                            break;
                    }
                }
            }
        }
    }
}
